import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:toys_flutter/components/AgeSpan.dart';
import 'package:toys_flutter/components/AuthSpan.dart';
import 'package:toys_flutter/components/LevelSpan.dart';
import 'package:toys_flutter/components/MyAvatarWidget.dart';
import 'package:toys_flutter/pages/UserDetail.dart';
import 'package:toys_flutter/utils/style_utils.dart';

class UserItem extends StatelessWidget {
  Map<String, dynamic>? user;
  UserItem({super.key, this.user});

  @override
  Widget build(BuildContext context) {
    if (user == null) {
      return const SizedBox();
    }

    final String avatar = user!['avatar'];
    final String userName = user!['nickname'];
    final String level = user!['level'].toString();

    return ListTile(
      leading: InkWell(
        onTap: () {
          // 跳转到用户主页
          Get.to(UserDetail(userId: user!['id'].toString()));
        },
        child: MyAvatarWidget(image: avatar),
      ),
      title: Row(
        children: [
          Text(userName, style: StyleUtils.mainTextStyle),
          const SizedBox(width: 5),
          LevelSpan(level: level),
          const SizedBox(width: 5),
          const AuthSpan(),
        ],
      ),
      subtitle: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const SizedBox(height: 5),
          Row(children: [
            AgeSpan(
              age: user!['age'].toString(),
              gender: user!['sex'].toString(),
            )
          ]),
          const SizedBox(height: 5),
          Text(
            user!['position']['position'],
            style: const TextStyle(fontSize: 12, color: Colors.black38),
          ),
        ],
      ),
      trailing: Column(
        mainAxisSize: MainAxisSize.min, // 确保列的最小高度
        crossAxisAlignment: CrossAxisAlignment.end, // 右对齐
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Row(
            mainAxisSize: MainAxisSize.min, // 确保行的最小宽度
            children: [
              Text(user!['position']['distance'].toString(),
                  style: const TextStyle(fontSize: 12, color: Colors.black38)),
              const SizedBox(width: 10),
              Icon(
                user!['online']['status'] == 'offline'
                    ? Icons.wifi_off
                    : Icons.wifi,
                size: 18,
                color: Colors.black54,
              ),
            ],
          ),
          Text(
            user!['online']['time'],
            style: const TextStyle(fontSize: 12, color: Colors.black38),
            overflow: TextOverflow.ellipsis,
            maxLines: 1,
          ),
        ],
      ),
    );
  }
}
